from machine import Pin

import time
import network

import requests


led1 = Pin(2, Pin.OUT)



def wifi_connect(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(False)
    wlan.active(True)

    start_time = time.time()

    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            led1.value(1)
            time.sleep(0.3)
            led1.value(0)
            time.sleep(0.3)

            if time.time() - start_time > 30:
                print('network config is complete!')
                break

    if wlan.isconnected():
        led1.value(1)
        print(f'network config is complete!{wlan.ifconfig()}')

        response = requests.get('https://www.baidu.com/')
        print(response.text)

if __name__ == '__main__':
    ssid = 'CMCC-有合租网嘛Q705301804'
    password = 'mm4016061'
    wifi_connect(ssid, password)
